<?php
/**
 * @author spenceryg
 * @date 2022/12/3
 * @time 3:40 下午
 * @focus love & life
 */


namespace app\auto\model;


use app\auto\lib\MethodGenerator;
use think\Model;

class Method extends \think\Model
{
    protected $table = 'auto_method';
    protected $jsonAssoc = true;
    protected $json = ['view_config'];

    public function methodField(): \think\model\relation\HasMany
    {
        return $this->hasMany(MethodFiled::class);
    }


    /**
     * controller
     * @return \think\model\relation\BelongsTo
     */
    public function controller(){
        return $this->belongsTo(Controller::class);
    }


    /**
     * onAfterUpdate
     * @param Model $model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function onAfterUpdate(Model $model): void
    {

    }


    /**
     * onBeforeDelete
     * @param Model $model
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function onBeforeDelete(Model $model)
    {
        MethodGenerator::deleteFromMethod($model);
        MethodFiled::destroy(function ($query) use ($model) {
            $query->where('method_id', $model->getAttr('id'));
        });
    }
}
